function getOneMessages (files) {
	let allMessage = {}
	files.keys().forEach(fPath => {
	   const componentConfig = files(fPath)
	   const fPaths = fPath.split('/')
	   const fileName = fPaths[fPaths.length - 1]
	   const strName = fileName.split('.')[0]
	   allMessage[strName] = componentConfig.default
	})
	return allMessage
}
function getVal(obj, path, defaultValue = undefined) {
  const travel = (regex) =>
    String.prototype.split
      .call(path, regex)
      .filter(Boolean)
      .reduce((o, k) => o === undefined ? o : o[k], obj);
  const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
  return result === undefined ? defaultValue : result;
}

let messages = {
  en: getOneMessages(require.context('./en/', true,  /\.js$/ )),
  zh_CN: getOneMessages(require.context('./zh_CN/', true,  /\.js$/ )),
  angola:getOneMessages(require.context('./angola/', true,  /\.js$/ )),
};
let currentLocale = uni.getStorageSync('locale_lang') || '';
if (!currentLocale) {
	const systemLanguage = uni.getSystemInfoSync().language;
	currentLocale = systemLanguage.startsWith('zh') ? 'zh_CN' : 'angola';
}
uni.setStorageSync('locale_lang', currentLocale)
const i18n = {
	t(key) {
		return getVal(messages,currentLocale+'.'+key) || key;
	},
	setLocale(locale) {
		if (locale && messages[locale]) {
			currentLocale = locale;
			// 切换语言后存储在本地，下次进入页面使用用户切换过的语言
			uni.setStorageSync('locale_lang', currentLocale);
		} else {
			console.warn(`Locale ${locale} not found`);
		}
	},
	getLocale(){
		return uni.getSystemInfoSync().language
	},
	getLangList () {
		const typeList = [
			{label:getVal(messages,currentLocale+'.common.chinese') || 'Chinese',value:'zh_CN'},
			{label:getVal(messages,currentLocale+'.common.english') || 'English',value:'en'},
			{label:getVal(messages,currentLocale+'.common.angola') || 'Angola',value:'angola'}
		]
		return typeList 
	}
};
export default i18n